package com.tangding.hprose.api.server.command.impl.watch;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tangding.cache.UserWatchingCache;
import com.tangding.common.exception.GException;
import com.tangding.common.logx.LogHelp;
import com.tangding.hprose.api.client.HproseAsClient;
import com.tangding.hprose.api.server.command.Command;
import com.tangding.hprose.api.server.command.Response;
import com.tangding.net.pool.BaseNettySessionPool;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月18日_下午3:34:48
 *
 */
@Component
public class QueryWatchingUsers implements Command {

	@Autowired
	BaseNettySessionPool pool;
	
	@Override
	public Response exe(Object... params) throws GException {
		@SuppressWarnings("unchecked")
		Map<String, Object> user = (Map<String, Object>) params[params.length - 1];
		String uid = (String) user.get("uid");
		Set<Object> needQueryIds = UserWatchingCache.getSet(uid);
//		System.out.println(needQueryIds);
		if (needQueryIds != null && needQueryIds.size() > 0) {
			Map<String, Object> request = new HashMap<>();
			request.put("uids", needQueryIds);
			request.put("onlineCondition", 0B11);
			request.put("typeCondition", 0B111);
			poolExe.submit(new Runnable() {
				public void run() {
					try {
						Object result = HproseAsClient.invoke("QueryUsers", request);
						pool.doResponse(uid, "NotifyUsers", result);
					} catch (GException e) {
						LogHelp.doLogAppErr(e);
					}
				}
			});
		}
		
		return doResponseSucc();
	}

}
